import 'package:flutter/material.dart';
import 'package:flutter_learn_vi/src/page/common/common_loading_dialog.dart';
import 'package:flutter_learn_vi/src/page/common/common_nodata_widget.dart';
import 'package:flutter_learn_vi/src/utils/navigator_utils.dart';
import 'package:get/get.dart';

import '../../../bean/bean_examine.dart';
import 'controller/examin_list_controller.dart';
import 'examine_score_recoder_page.dart';

class ExaminListPage extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return ExaminListPageState();
  }
}

class ExaminListPageState extends State<ExaminListPage>
    with SingleTickerProviderStateMixin {
  ExaminListController goodsCenterController = Get.put(ExaminListController());
  TabController _tabController;

  @override
  void initState() {
    super.initState();
    _tabController = new TabController(length: 3, vsync: this);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("我的考核"),
        elevation: 0,
        bottom: TabBar(
          indicatorColor: Colors.red,
          controller: _tabController,
          onTap: (int index) {
            Get.find<ExaminListController>().onRefresh(index: index);
          },
          tabs: [
            Tab(text: '全部'),
            Tab(text: '进行中'),
            Tab(text: '已完成'),
          ],
        ),
      ),
      body: RefreshIndicator(
        onRefresh: () {
          return Get.find<ExaminListController>().onPullRefresh();
        },
        child: GetBuilder(
          id: "examinList",
          builder: (ExaminListController controller) {
            List<ExamineUserBean> goodsList = controller.goodsList;
            if (controller.isLoading) {
              return LoadingWidget();
            }
            if (goodsList.length == 0) {
              return SizedBox(
                width: double.infinity,
                height: Get.height / 3 * 2,
                child: Center(
                  child: NoDataWidget(
                    clickCallBack: () {
                      Get.find<ExaminListController>().onPullRefresh();
                    },
                    text: "暂无考核内容",
                  ),
                ),
              );
            }
            return ListView.builder(
              itemCount: goodsList.length,
              itemBuilder: (BuildContext context, int index) {
                ExamineUserBean item = goodsList[index];

                //是否达标 0未统计  1达标 2未达标
                num examineStandard = item.examineStandard;
                Color examineStandardColor = Colors.grey;
                String examineStandardStr = "";
                if (examineStandard == 0) {
                  examineStandardStr = "未统计";
                  examineStandardColor = Colors.grey;
                } else if (examineStandard == 1) {
                  examineStandardStr = "已达标";
                  examineStandardColor = Colors.green;
                } else if (examineStandard == 2) {
                  examineStandardStr = "未达标";
                  examineStandardColor = Colors.red;
                } else if (examineStandard == 3) {
                  examineStandardStr = "已完成";
                  examineStandardColor = Colors.grey;
                } else {
                  examineStandardStr = "--";
                }

                return Container(
                  color: Colors.white,
                  margin: EdgeInsets.only(bottom: 10),
                  padding:
                      EdgeInsets.only(left: 12, right: 12, top: 12, bottom: 2),
                  child: Container(
                    alignment: Alignment.center,
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Row(
                          crossAxisAlignment: CrossAxisAlignment.center,
                          mainAxisSize: MainAxisSize.min,
                          children: [
                            Image.asset(
                              "assets/images/3.0x/examine_doing.png",
                              width: 18,
                            ),
                            SizedBox(width: 10),
                            Expanded(
                              child: Text(
                                item.examineConfig.examineTitle,
                                style: TextStyle(
                                    color: Color(0xff333333),
                                    fontSize: 16,
                                    fontWeight: FontWeight.w500),
                              ),
                            ),
                            Text(item.examineConfig.examineStatuesStr),
                          ],
                        ),
                        SizedBox(height: 12),
                        Container(
                          child: Text("考核规则"),
                        ),
                        Container(
                          color: Color(0xfafafafa),
                          padding: EdgeInsets.all(10),
                          child: Column(
                            mainAxisSize: MainAxisSize.min,
                            children: [
                              Row(children: [
                                Text("考核开始 时间"),
                                Text("${item.examineBeginTime}"),
                              ]),
                              SizedBox(height: 2),
                              Row(
                                children: [
                                  Text("考核结束 时间"),
                                  Text("${item.examineEndTime}"),
                                ],
                              ),
                              SizedBox(height: 2),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisSize: MainAxisSize.min,
                                children: [
                                  Text("细则描述"),
                                  SizedBox(height: 2),
                                  Container(
                                    padding: EdgeInsets.only(left: 10),
                                    child: Text(
                                      "${item.examineConfig.examineRule}",
                                      style: TextStyle(
                                          color: Colors.grey, fontSize: 12),
                                    ),
                                  ),
                                ],
                              ),
                            ],
                          ),
                        ),
                        SizedBox(height: 10),
                        Container(
                          child: Row(
                            children: [
                              Expanded(child: Text("我的本次考核统计")),
                              Container(
                                padding: EdgeInsets.only(
                                    left: 6, right: 6, top: 1, bottom: 1),
                                decoration: BoxDecoration(
                                    color: examineStandardColor,
                                    borderRadius:
                                        BorderRadius.all(Radius.circular(4))),
                                child: Text(
                                  examineStandardStr,
                                  style: TextStyle(
                                      fontSize: 12, color: Colors.white),
                                ),
                              )
                            ],
                          ),
                        ),
                        buildMyStatScoreContainer(item),
                      ],
                    ),
                  ),
                );
              },
            );
          },
        ),
      ),
    );
  }

  Widget buildMyStatScoreContainer(ExamineUserBean item) {
    if (item.examineStandard == 0) {
      return Container(
        height: 10,
      );
    }
    return GestureDetector(
      onTap: () {
        NavigatorUtils.pushPage(
            context,
            ExamineScoreRecoderPage(
                userId: item.userId, configId: item.examineConfig.id));
      },
      child: Container(
        color: Color(0xfafafafa),
        padding: EdgeInsets.all(10),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Row(children: [
              Row(
                mainAxisSize: MainAxisSize.min,
                children: [
                  Text("平均分 "),
                  Text("${item.examineAvg} 分"),
                ],
              ),
              Expanded(
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Text("统计时间  "),
                    Text("${item.statisticsTime}"),
                  ],
                ),
              ),
            ]),
            SizedBox(height: 4),
            Row(
              children: [
                Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text("最高分 "),
                    Text("${item.examineMax} 分"),
                  ],
                ),
                Expanded(
                    child: Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    Text("参与考试次数 "),
                    Text("${item.examineCountValid} 次"),
                  ],
                )),
              ],
            ),
            SizedBox(height: 4),
            Row(
              children: [
                Row(
                  mainAxisSize: MainAxisSize.min,
                  children: [
                    Text("最低分 "),
                    Text("${item.examineMin} 分"),
                  ],
                ),
                Expanded(
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.end,
                    children: [
                      Text("有效考试次数 "),
                      Text("${item.examineCount} 次"),
                    ],
                  ),
                )
              ],
            ),
            SizedBox(height: 8),
          ],
        ),
      ),
    );
  }
}
